package com.gnerv.sylvanas.business.manage.autoconfigure;

import com.gnerv.sylvanas.framework.core.BaseAccountPassword;
import com.gnerv.sylvanas.framework.core.BaseCurrentAccount;
import com.gnerv.sylvanas.framework.core.BaseUkIdGenerate;
import com.gnerv.sylvanas.framework.core.init.sql.BaseDatabaseInitService;
import com.gnerv.sylvanas.module.mybatis.interceptor.CommonParameterHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@AutoConfiguration
@ConditionalOnWebApplication
@EnableConfigurationProperties(value = {FrameworkManageConfigProperties.class})
public class FrameworkManageAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public BaseUkIdGenerate baseUkIdGenerate() {
        return new BaseUkIdGenerate(){};
    }


    @Bean
    @ConditionalOnMissingBean
    public BaseAccountPassword baseAccountPassword() {
        return new BaseAccountPassword(){
            @Override
            public String encrypt(String source) {
                return source;
            }
        };
    }

    @Bean
    @ConditionalOnMissingBean
    public BaseCurrentAccount baseCurrentAccount() {
        return new BaseCurrentAccount() {
            @Override
            public String username() {
                return "";
            }

            @Override
            public String ukId() {
                return "";
            }
        };
    }

    @Bean
    public CommonParameterHandler currentAccountUkId(BaseCurrentAccount baseCurrentAccount) {
        return new CommonParameterHandler() {
            @Override
            public Map<String, Object> getParameters() {
                Map<String, Object> paramMap = new HashMap<>();
                paramMap.put("currentAccountUkId", baseCurrentAccount.ukId());
                return paramMap;
            }
        };
    }

    @Bean
    public BaseDatabaseInitService manageDatabaseInitService() {
        return new BaseDatabaseInitService() {
            @Override
            public List<String> schemaLocationList() {
                return List.of("classpath*:db/schema.sql");
            }

            @Override
            public List<String> dataLocationList() {
                return List.of("classpath*:db/data.sql");
            }
        };
    }

}
